package com.crm.business.crm.controller;


import com.crm.business.crm.service.CrmUploadExcelService;
import com.crm.business.crm.service.ICrmPutinStatisticsService;
import com.crm.common.crm.common.CrmModel;
import com.crm.common.crm.constant.CrmCodeEnum;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.entity.BO.CrmRoiSearchBO;
import com.crm.common.crm.entity.BO.CrmUpdateInformationBO;
import com.crm.common.crm.entity.BO.UploadExcelBO;
import com.crm.common.crm.entity.PO.CrmPutinStatistics;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.common.ka.core.exception.CrmException;
import com.crm.business.utils.UserUtil;
import com.crm.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/crmPutinStatistics")
@Api(tags = "投放统计模块接口")
public class CrmPutinStatisticsController {

    @Autowired
    private ICrmPutinStatisticsService crmPutinStatisticsService;

    @Autowired
    private CrmUploadExcelService uploadExcelService;

    @PostMapping("/queryById/{putinStatisticsId}")
    @ApiOperation("根据ID查询")
    public Result<CrmModel> queryById(@PathVariable("putinStatisticsId") @ApiParam(name = "id", value = "id") Integer putinStatisticsId) {
        Integer number = crmPutinStatisticsService.lambdaQuery().eq(CrmPutinStatistics::getPutinStatisticsId,putinStatisticsId).count();
        if (number == 0){
            throw new CrmException(CrmCodeEnum.CRM_DATA_DELETED,"投放金额");
        }
        CrmModel model = crmPutinStatisticsService.queryById(putinStatisticsId);
        return R.ok(model);
    }

    @PostMapping("/deleteByIds")
    @ApiOperation("根据ID删除数据")
//    @SysLogHandler(behavior = BehaviorEnum.DELETE)
    public Result deleteByIds(@ApiParam(name = "ids", value = "id列表") @RequestBody List<Integer> ids) {
        crmPutinStatisticsService.deleteByIds(ids);
        return R.ok();
    }

    @PostMapping("/queryPageList")
    @ApiOperation("查询列表页数据")
    @ResponseBody
    public Result<BasePage<CrmPutinStatistics>> queryPageList(@RequestBody CrmRoiSearchBO search) {
        if (StringUtils.isNotBlank(search.getCategoryStr())) {
            String[] categories = search.getCategoryStr().split(",");
            int[] array = Arrays.asList(categories).stream().mapToInt(Integer::parseInt).toArray();
            search.setCategorys(array);
        }
        BasePage<CrmPutinStatistics> mapBasePage = crmPutinStatisticsService.queryPageList(search);
        return R.ok(mapBasePage);
    }

    @PostMapping("/add")
    @ApiOperation("保存数据")
//    @SysLogHandler(behavior = BehaviorEnum.SAVE,object = "#crmModel.entity[leadsName]",detail = "'新增了线索:' + #crmModel.entity[leadsName]")
    public Result add(@RequestBody CrmPutinStatistics crmPutinStatistics) {
        crmPutinStatisticsService.addOrUpdate(crmPutinStatistics);
        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改数据")
//    @SysLogHandler(behavior = BehaviorEnum.UPDATE)
    public Result update(@RequestBody CrmPutinStatistics crmPutinStatistics) {
        crmPutinStatisticsService.addOrUpdate(crmPutinStatistics);
        return R.ok();
    }



    @PostMapping("/uploadExcel")
    @ApiOperation("导入投放金额")
//    @SysLogHandler(behavior = BehaviorEnum.EXCEL_IMPORT,object = "导入投放金额",detail = "导入投放金额")
    public Result<Long> uploadExcel(@RequestParam("file") MultipartFile file, @RequestParam("ownerUserId") Long ownerUserId, @RequestParam("repeatHandling") Integer repeatHandling) {
        UploadExcelBO uploadExcelBO = new UploadExcelBO();
        uploadExcelBO.setOwnerUserId(ownerUserId);
        uploadExcelBO.setUserInfo(UserUtil.getUser());
        uploadExcelBO.setCrmEnum(CrmEnum.ROI_STATISTICS);
        uploadExcelBO.setPoolId(null);
        uploadExcelBO.setRepeatHandling(repeatHandling);
        Long messageId = uploadExcelService.uploadExcel(file, uploadExcelBO);
        return R.ok(messageId);
    }

    @PostMapping("/updateInformation")
    @ApiOperation("基本信息保存修改")
//    @SysLogHandler(behavior = BehaviorEnum.UPDATE)
    public Result updateInformation(@RequestBody CrmUpdateInformationBO updateInformationBO) {
        crmPutinStatisticsService.updateInformation(updateInformationBO);
        return R.ok();
    }

    @PostMapping("/roiSatistics")
    @ApiOperation("ROI统计")
    public Result roiSatistics() {
        crmPutinStatisticsService.roiSatistics();
        return R.ok();
    }

    @PostMapping("/allExportExcel")
    @ApiOperation("全部导出")
//    @SysLogHandler(behavior = BehaviorEnum.EXCEL_EXPORT,object = "ROI投放金额导出",detail = "全部导出")
    public void allExportExcel(@RequestBody CrmRoiSearchBO search, HttpServletResponse response) {
        search.setPageType(0);
        crmPutinStatisticsService.exportExcel(response, search);
    }

    @PostMapping("/batchExportExcel")
    @ApiOperation("选中导出")
//    @SysLogHandler(behavior = BehaviorEnum.EXCEL_EXPORT,object = "ROI投放金额导出",detail = "选中导出")
    public void batchExportExcel(@RequestBody CrmRoiSearchBO search, HttpServletResponse response) {
       // CrmRoiSearchBO search = new CrmRoiSearchBO();
         String allPutinPlatform = search.getPutinPlatform();
         search.setPutinPlatforms(allPutinPlatform.split(","));
         search.setPageType(0);
         search.setStartDate(search.getStartDate().split(",")[0]);
         search.setEndDate(search.getEndDate().split(",")[0]);
         crmPutinStatisticsService.exportExcel(response, search);
    }

    @PostMapping("/querySource")
    @ApiOperation("查询投放平台")
    public Result<List<String>> querySource() {
        List<String> list = crmPutinStatisticsService.querySource();
        return R.ok(list);
    }
}
